package com.efast.dmom.mes.datahandlers.entity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Param;
import com.efast.dmom.mes.bean.ParamGroup;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.ParamGroupDao;
import com.efast.dmom.mes.datahandlers.base.ConfigurationDataHandler;
import com.efast.dmom.mes.entity.ParamGroupEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class ParamGroupHandler extends ConfigurationDataHandler<ParamGroup>{
	@Autowired
	private ParamGroupDao paramGroupDao;
	@Autowired
	private ParamHandler paramHandler;
	
	@Override
	public ResultBean save(ParamGroup obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(1);
			paramGroupDao.save(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean update(ParamGroup obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			paramGroupDao.update(obj.getInnerData());
		}
		return resultBean;
	}
	
	@Override
	public ResultBean delete(ParamGroup obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());		
			paramGroupDao.delete(obj.getInnerData());
		}
		return resultBean;
	}
	
	public ParamGroup getByNameAndPlantIdAndGlobal(String name,long plantId,int isGlobal) {
		ParamGroup paramGroup = new ParamGroup();
		ParamGroupEntity paramGroupEntity = paramGroupDao.getByNameAndPlantIdAndGlobal(name, plantId, isGlobal);
		if(paramGroupEntity!=null) {
			paramGroup.setInnerData(paramGroupEntity);
			 List<Param> list = paramHandler.getByParamGroupId(paramGroupEntity.getId());
			if(list!=null && list.size()>0) {
				paramGroup.setParams(list);
				return paramGroup;
			}
		}
		return paramGroup;
	}
	
	public ParamGroup getById(long paramGroupId) {
		ParamGroup paramGroup = new ParamGroup();
		ParamGroupEntity paramGroupEntity = paramGroupDao.getById(paramGroupId);
		if(paramGroupEntity!=null) {
			paramGroup.setInnerData(paramGroupEntity);
		}
		return paramGroup;
	}

	@Override
	public ResultBean saveBatch(List<ParamGroup> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<ParamGroup> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<ParamGroup> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
